{include file="public/layout" /}
<body class="bodystyle" style="cursor: default; -moz-user-select: inherit;">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    {include file="tools/bar" /}
    <!-- 操作说明 -->
    <div id="explanation" class="explanation" style="color: rgb(44, 188, 163); background-color: rgb(237, 251, 248); width: 99%; height: 100%;">
        <div id="checkZoom" class="title"><i class="fa fa-lightbulb-o"></i>
            <h4 title="提示相关设置操作时应注意的要点">提示</h4>
            <span title="收起提示" id="explanationZoom" style="display: block;"></span>
        </div>
        <ul>
            <li>数据备份功能根据你的选择备份全部数据或指定数据，导出的数据文件可用“数据恢复”功能或 phpMyAdmin 导入</li>
            <li>建议定期备份数据库</li>
        </ul>
    </div>
    <div class="flexigrid">
        <div class="mDiv">
            {eq name="$Think.const.CONTROLLER_NAME.'@export'|is_check_access" value="1"}
                <div class="fbutton">
                    <a id="ing_btn">
                        <div class="add" title="数据备份" onclick="$('.export_btn').trigger('click');">
                            <span><i class="fa fa-book"></i><span>数据备份</span></span>
                        </div>
                    </a>
                </div>
            {/eq}
        </div>
        <div class="hDiv">
            <div class="hDivBox">
                <table cellspacing="0" cellpadding="0" style="width: 100%">
                    <thead>
                    <tr>
                        <th class="sign w40" axis="col0">
                            <div class="tc"><input type="checkbox" autocomplete="off" onclick="javascript:$('input[name*=tables]').prop('checked',this.checked);" checked="checked"></div>
                        </th>
                        <th abbr="article_title" axis="col3">
                            <div style="padding-left: 10px;" class="">数据库表</div>
                        </th>
                        <th abbr="ac_id" axis="col4" class="w80">
                            <div class="tc">记录条数</div>
                        </th>
                        <th abbr="article_show" axis="col5" class="w80">
                            <div class="tc">占用空间</div>
                        </th>
                        <th abbr="article_time" axis="col6" class="w120">
                            <div class="tc">编码</div>
                        </th>
                        <th abbr="article_time" axis="col6" class="w160">
                            <div class="tc">创建时间</div>
                        </th>
                        <th abbr="article_time" axis="col6" class="w80">
                            <div class="tc">备份状态</div>
                        </th>
                        <th axis="col1" class="w80">
                            <div class="tc">操作</div>
                        </th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <div class="bDiv" style="height: auto;">
            <div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
                <form  method="post" id="export-form" action="{:url('Tools/export')}">
                    <table id="tb_flexigrid" style="width: 100%">
                        <tbody>
                        {empty name="list"}
                            <tr>
                                <td class="no-data" align="center" axis="col0" colspan="50">
                                    <i class="fa fa-exclamation-circle"></i>没有符合条件的记录
                                </td>
                            </tr>
                        {else/}
                            {foreach name="list" item="vo" key="k" }
                            <tr data-id="{$vo.Name}">
                                <td class="sign">
                                    <div class="w40 tc"><input type="checkbox" autocomplete="off" name="tables[]" value="{$vo.Name}" checked="checked"></div>
                                </td>
                                <td style="width: 100%">
                                    <div style="padding-left: 10px;">{$vo.Name}</div>
                                </td>
                                <td>
                                    <div class="w80 tc">{$vo.Rows}</div>
                                </td>
                                <td>
                                    <div class="w80 tc">{$vo.Data_length|format_bytes}</div>
                                </td>
                                <td>
                                    <div class="w120 tc">{$vo.Collation}</div>
                                </td>
                                <td>
                                    <div class="w160 tc">{$vo.Create_time}</div>
                                </td>
                                <td>
                                    <div class="info w80 tc">未备份</div>
                                </td>
                                <td>
                                    <div class="w80 tc">
                                        {eq name="$Think.const.CONTROLLER_NAME.'@optimize'|is_check_access" value="1"}
                                        <!-- <a href="{:url('Tools/optimize',array('tablename'=>$vo['Name']))}" class="btn blue"><i class="fa fa-magic"></i>优化</a> -->
                                        {/eq}
                                        {eq name="$Think.const.CONTROLLER_NAME.'@repair'|is_check_access" value="1"}
                                        <a class="btn green" href="{:url('Tools/repair',array('tablename'=>$vo['Name']))}"><i class="fa fa-wrench"></i>修复</a>
                                        {/eq}
                                    </div>
                                </td>
                               
                            </tr>
                            {/foreach}
                        {/empty}
                        </tbody>
                    </table>
                </form>
            </div>
            <div class="iDiv" style="display: none;"></div>
        </div>
        <div class="tDiv">
            <div class="tDiv2">
                <div class="fbutton checkboxall">
                    <input type="checkbox" autocomplete="off" onclick="javascript:$('input[name*=tables]').prop('checked',this.checked);" checked="checked">
                </div>
                {eq name="$Think.const.CONTROLLER_NAME.'@export'|is_check_access" value="1"}
                <div class="fbutton export_btn">
                    <a id="ing_btn" class="layui-btn layui-btn-primary">
                        <span>数据备份</span>
                    </a>
                </div>
                {/eq}
				<div class="fbuttonr" style="margin-right: 15px;">
					<div class="total">
						 <h5>共{$tableNum}条数据，共计{$total}</h5>
					</div>
				</div>
            </div>
            <div style="clear:both"></div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function(){
        // 表格行点击选中切换
        $('#tb_flexigrid >tbody >tr').click(function(){
            $(this).toggleClass('trSelected');
        });

        // 点击刷新数据
        $('.fa-refresh').click(function(){
            location.href = location.href;
        });

    });

    (function($){
        var $form = $("#export-form"), $export = $(".export_btn"), tables
        $export.click(function(){
            if($("input[name^='tables']:checked").length == 0){
                layer.alert('请选中要备份的数据表', {icon: 5, title:false, closeBtn:false});
                return false;
            }
            $export.addClass("disabled");
            $export.find('a').html("正在发送备份请求...");
            $.post(
                "{:url('Tools/export', ['_ajax'=>1])}",
                $form.serialize(),
                function(res){
                    if(res.code){
                        tables = res.tables;
                        var loading = layer.msg('正在备份表(<font id="upgrade_backup_table">'+res.tab.table+'</font>)……<font id="upgrade_backup_speed">0.01</font>%', 
                        {
                            icon: 1,
                            time: 3600000, //1小时后后自动关闭
                            shade: [0.2] //0.1透明度的白色背景
                        });
                        $export.find('a').html(res.msg + "开始备份，请不要关闭本页面！");
                        backup(res.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        layer.alert(res.msg, {icon: 5, title:false, closeBtn:false});
                        $export.removeClass("disabled");
                        $export.find('a').html("立即备份");
                    }
                },
                "json"
            );
            return false;
        });
        
        function backup(tab, status){
            status && showmsg(tab.id, "开始备份……(0%)");
            $.post("{:url('Tools/export', ['_ajax'=>1])}", tab, function(res){
                if(res.code){
                    if (tab.table) {
                        showmsg(tab.id, res.msg);
                        $('#upgrade_backup_table').html(tab.table);
                        $('#upgrade_backup_speed').html(tab.speed);
                        $export.find('a').html('初始化成功！正在备份表('+tab.table+')……'+tab.speed+'%，请不要关闭本页面！');
                    } else {
                        $export.find('a').html('初始化成功！开始备份……，请不要关闭本页面！');
                    }
                    if(!$.isPlainObject(res.tab)){
                        var loading = layer.msg('备份完成……100%，请不要关闭本页面！', 
                        {
                            icon: 1,
                            time: 2000, //1小时后后自动关闭
                            shade: [0.2] //0.1透明度的白色背景
                        });
                        $export.removeClass("disabled");
                        $export.find('a').html("备份完成……100%，点击重新备份");
                        setTimeout(function(){
                            layer.closeAll();
                            layer.alert('备份成功！', {icon: 6, title:false, closeBtn:false});
                        }, 1000);
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    setTimeout(function () {
                        backup(res.tab, tab.id != res.tab.id);
                    }, 350);
                } else {
                    layer.closeAll();
                    $export.removeClass("disabled");
                    $export.find('a').html("立即备份");
                }
            }, "json");
        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>

{include file="public/footer" /}